package com.ctparking;

import java.util.List;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Point;
import android.os.Bundle;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;

public class ParkingMap extends MapActivity{
	
	 MapView mapView; 
	   private MapController mc;
	   GeoPoint p;
	    
	    class MapOverlay extends com.google.android.maps.Overlay
	    {   
	    	 @Override
	         public boolean draw(Canvas canvas, MapView mapView, 
	         boolean shadow, long when) 
	         {
	             super.draw(canvas, mapView, shadow);                   
	  
	             //---translate the GeoPoint to screen pixels---
	             Point screenPts = new Point();
	             mapView.getProjection().toPixels(p, screenPts);
	  
	             //---add the marker---
	             Bitmap bmp = BitmapFactory.decodeResource(
	                 getResources(), R.drawable.marker_parcheggio);            
	             canvas.drawBitmap(bmp, screenPts.x, screenPts.y-47, null);         
	             return true;
	         }
	    	
	    	 /*
	    	 @Override
	         public boolean onTouchEvent(MotionEvent event, MapView mapView) 
	         {   
	             //---when user lifts his finger---
	             if (event.getAction() == 1) {                
	                 GeoPoint p = mapView.getProjection().fromPixels(
	                     (int) event.getX(),
	                     (int) event.getY());
	  
	                 Geocoder geoCoder = new Geocoder(
	                     getBaseContext(), Locale.getDefault());
	                 try {
	                     List<Address> addresses = geoCoder.getFromLocation(
	                         p.getLatitudeE6()  / 1E6, 
	                         p.getLongitudeE6() / 1E6, 1);
	  
	                     String add = "";
	                     if (addresses.size() > 0) 
	                     {
	                         for (int i=0; i<addresses.get(0).getMaxAddressLineIndex(); 
	                              i++)
	                            add += addresses.get(0).getAddressLine(i) + "\n";
	                     }
	  
	                     Toast.makeText(getBaseContext(), add, Toast.LENGTH_SHORT).show();
	                 }
	                 catch (IOException e) {                
	                     e.printStackTrace();
	                 }   
	                 return true;
	             }
	             else                
	                 return false;
	         } */       
	    } 
	    
  /** Called when the activity is first created. */    
	@Override
  public void onCreate(Bundle savedInstanceState)
  {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.parkingmap);
      mapView = (MapView) findViewById(R.id.mapView);
      mc = mapView.getController();
      
      // String coordinates[] = {"1.352566007", "103.78921587"};
      double lat = Database.parcheggio_scelto.getLatitudine();
      double lng = Database.parcheggio_scelto.getLongitudine();
      
      p = new GeoPoint((int) (lat), (int) (lng));

      mc.animateTo(p);
      mapView.setBuiltInZoomControls(true);
      mc.setZoom(14);
      //---Add a location marker---
      MapOverlay mapOverlay = new MapOverlay();
      List<Overlay> listOfOverlays = mapView.getOverlays();
      listOfOverlays.clear();
      listOfOverlays.add(mapOverlay);    
  }

  @Override
  protected boolean isRouteDisplayed() {
      return false;
  }
}
